import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
/**
 * Generated class for the Task page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-task',
	templateUrl: 'task.html',
})
export class TaskPage {

	private selectIndex: any;
	/**
	 * 列表
	 */
	private listItems1Page: any;
	private listItems1: Array<{
		id: any,
		title: any,
		content: any,
		status: any,
		publicBy: any,
		finishDate: any,
		publicDate: any,
	}>;
	/**
 * 列表
 */
	private listItems2Page: any;
	private listItems2: Array<{
		id: any,
		title: any,
		content: any,
		status: any,
		publicBy: any,
		finishDate: any,
		publicDate: any,
	}>;

	/**
	 * 选择框
	 */
	private inputSearch = {
		title: '',
		member: new Object(),
		company: new Object(),
		depart: new Object(),
		startFileDate: '',
		endFileDate: ''
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {
		this.selectIndex = "0";
		this.listItems1 = new Array();
		this.listItems2 = new Array();
		this.listItems1Page = 1;
		this.listItems2Page = 1;
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad Task');
		this.loadData("0");
	}

	//切换
	segmentChanged(ev: any) {
		if (ev['value'] == 0) {
			this.loadData("0");
			this.selectIndex = "0";
		} else {
			this.loadData("1");
			this.selectIndex = "1";
		}
	}

	loadData(index: any) {
		//异步方式
		return new Promise((success) => {
			//登录成功
			this.storage.getStorage('user').then((val) => {
				if (index == "0") {
					this.listItems1Page = 1;
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems1Page,
						title: this.inputSearch.title,
						member: this.inputSearch.member,
						company: this.inputSearch.company,
						depart: this.inputSearch.depart,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//网络请求
					this.http.postJson('homeTodo', 'taskmanage.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData(index, true, datas);
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				} else {
					this.listItems2Page = 1;
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems2Page,
						title: this.inputSearch.title,
						member: this.inputSearch.member,
						company: this.inputSearch.company,
						depart: this.inputSearch.depart,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//网络请求
					this.http.postJson('homeTodo', 'taskmanage.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData(index, true, datas);
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				}
			});
		});
	}
	//加载更多
	loadMoreData(index: any) {
		//异步方式
		return new Promise((success) => {
			//初始化
			this.storage.getStorage("user").then(val => {
				if (index == "0") {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems1Page + 1,
						title: this.inputSearch.title,
						member: this.inputSearch.member,
						company: this.inputSearch.company,
						depart: this.inputSearch.depart,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//收文传阅列表
					this.http.postJson('homeTodo', 'taskmanage.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData(index, false, datas);
							if (datas.length > 0)
								this.listItems1Page++;
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				} else {
					//请求方式如下
					let params = {
						user_id: val['user_id'],
						pageNo: this.listItems2Page + 1,
						title: this.inputSearch.title,
						member: this.inputSearch.member,
						company: this.inputSearch.company,
						depart: this.inputSearch.depart,
						startFileDate: this.inputSearch.startFileDate,
						endFileDate: this.inputSearch.endFileDate
					};
					//收文传阅列表
					this.http.postJson('homeTodo', 'taskmanage.do', params).then(res => {
						if (res['data'] != undefined) {
							let datas = res['data']['data'];
							this.loadMakeData(index, false, datas);
							if (datas.length > 0)
								this.listItems2Page++;
							if (success)
								success();
						} else {
							if (success)
								success();
						}
					});
				}
			});
		});
	}

	//共通列表
	loadMakeData(index: any, isnew: any, datas: any) {
		if (index == "0") {
			if (isnew)
				this.listItems1 = new Array();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItems1.push({
					id: item['id'],
					title: item['subject'],
					content: item['content'],
					status: item['status'],
					publicBy: item['publicBy'],
					finishDate: item['finishDate'],
					publicDate: item['publicDate'],
				});
			}
		} else {
			if (isnew)
				this.listItems2 = new Array();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.listItems2.push({
					id: item['id'],
					title: item['subject'],
					content: item['content'],
					status: item['status'],
					publicBy: item['publicBy'],
					finishDate: item['finishDate'],
					publicDate: item['publicDate'],
				});
			}
		}
	}

	//搜索
	dosearch() {
		let data = new Object();
		let params = new Object();
		params['applyapplytitle'] = this.inputSearch.title;
		params['applyapplymember'] = this.inputSearch.member;
		params['applyapplycompany'] = this.inputSearch.company;
		params['applyapplydepart'] = this.inputSearch.depart;
		params['applystarttime'] = this.inputSearch.startFileDate;
		params['applyendtime'] = this.inputSearch.endFileDate;
		data['type'] = 7; //检索类型
		data['params'] = params; //参数传递
		let opts = new Object();
		this.dialog.showModal("ModalsearchPage", data, opts).then(success => {
			//界面关闭的时候
			if (success) {
				this.inputSearch.title = success['applyapplytitle'];
				this.inputSearch.member = success['applyapplymember'];
				this.inputSearch.company = success['applyapplycompany'];
				this.inputSearch.depart = success['applyapplydepart'];
				this.inputSearch.startFileDate = success['applystarttime'];
				this.inputSearch.endFileDate = success['applyendtime'];
				this.loadData(this.selectIndex);
			}
		});
	}

	/*下拉加载*/
	doRefresh(refresher) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			/*判断是哪个列表显示*/
			this.loadData(this.selectIndex).then(success => {
				refresher.complete();
			});
		}, 1500);
	}
	/*上拉加载*/
	doInfinite(infiniteScroll) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			this.loadMoreData(this.selectIndex).then(success => {
				infiniteScroll.complete();
			});
		}, 1500);
	}

	//详情
	dodetail(item: any) {
		this.navCtrl.push('TaskdetailPage', {
			type: 6
		});
	}
}